package com.framework.dubbo.producer.file2html;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * 描述：
 * 源文形式
 * @author xianggj
 * @Date 2021/8/26 9:55
 **/
public class HtmlPreviewUtil extends AbstractFilePreviewTools {
    @Override
    public String convert2Html(String path) {
        return FileUtil.readString(path, Charset.forName("UTF-8"));
    }

    @Override
    public String convert2Html(InputStream in, String ext) {
        String result = IoUtil.readUtf8(in);
        return result;
    }

    @Override
    public String convert2Str(InputStream in, String ext) {
        String result = IoUtil.readUtf8(in);
        result=  HtmlUtil.cleanHtmlTag(HtmlUtil.removeHtmlTag(result, "script", "style"));
        return StrUtil.cleanBlank(result);
    }

    @Override
    public String convert2Html(File file, String ext) {
        FileReader fileReader = new FileReader(file);
        String result = fileReader.readString();
        return result;
    }

    public static void main(String[] args) throws FileNotFoundException {
        File sourceFile = new File("C:\\Users\\ZHAOJIAN\\Desktop\\测试文件\\index.html");
        FileInputStream fis = new FileInputStream(sourceFile);
        System.out.println(new HtmlPreviewUtil().convert2Str(fis, "html"));
    }
}
